package com.ship.dispatch.service.fob.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.core.utils.StringUtils;
import com.ship.dispatch.bean.fob.SpFobBaseBean;
import com.ship.dispatch.bean.fob.SpFobFileRecord;
import com.ship.dispatch.bean.fob.SpFobSubleaseDeliveryUpdate;
import com.ship.dispatch.bean.fob.SpFobSubleaseRedeliveryInfo;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.bpm.vo.ManageEntityVo;
import com.ship.dispatch.mapper.fob.SpFobSubleaseDeliveryUpdateMapper;
import com.ship.dispatch.mapper.fob.SpFobSubleaseRedeliveryInfoMapper;
import com.ship.dispatch.service.SpDispatchSchemeExecuteService;
import com.ship.dispatch.service.fob.SpFobFileRecordService;
import com.ship.dispatch.service.fob.SpFobSubleaseRedeliveryInfoService;
import com.ship.dispatch.service.fob.SpFobTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpFobSubleaseRedeliveryInfoServiceImpl extends FobBaseServiceImpl<SpFobSubleaseRedeliveryInfoMapper, SpFobSubleaseRedeliveryInfo> implements SpFobSubleaseRedeliveryInfoService {

    @Autowired(required = false)
    SpFobSubleaseRedeliveryInfoMapper mapperapper;

    @Autowired
    SpFobFileRecordService spFobFileRecordService;

    @Autowired
    SpDispatchSchemeExecuteService schemeDetailService;

    @Autowired
    SpFobTemplateService spFobTemplateService;

    @Override
    public void updateEntity(ManageEntityVo manageEntityVo, String json) {
        SpFobSubleaseRedeliveryInfo object = JsonUtils.jsonToObject(json,SpFobSubleaseRedeliveryInfo.class);
        SpFobSubleaseRedeliveryInfo his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryInfo>().eq(SpFobSubleaseRedeliveryInfo::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSubleaseRedeliveryInfo::getStatus,-2));
        if(his==null){ // 新增
            object.setSchemeDetailId(manageEntityVo.getSchemeDetailId());
            object.setTaskId(manageEntityVo.getTaskId());
            object.setProceeInstanceId(manageEntityVo.getProcessInstanceId());
            mapperapper.insert(object);
        }else { // 修改
            object.setId(his.getId());
            BeanUtils.copyProperties(object,his);
            mapperapper.updateById(his);
            // 处理多余文件记录
            List<String> ids = new ArrayList<>();
            if(his.getNoticeFile()!=null && his.getNoticeFile().length()>0){
                String[] fieldIds = his.getNoticeFile().split(",");
                ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
            }
            spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
        }

    }

    @Override
    public SpFobBaseBean getBaseBeanByInstanceId(String id) {
        return mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryInfo>().eq(SpFobSubleaseRedeliveryInfo::getProceeInstanceId,id).ne(SpFobSubleaseRedeliveryInfo::getStatus,-2));
    }
    @Override
    public void  saveEntity(SpFobBaseBean bean){
        SpFobSubleaseRedeliveryInfo spFob = new SpFobSubleaseRedeliveryInfo();
        BeanUtils.copyProperties(bean,spFob);
        this.save(spFob);
    }

    @Override
    public void updateEntityById(SpFobBaseBean bean) {
        SpFobSubleaseRedeliveryInfo spFob = new SpFobSubleaseRedeliveryInfo();
        BeanUtils.copyProperties(bean,spFob);
        this.updateById(spFob);
    }


    @Override
    public Object getFormData(String id,Long serviceId) {
        SpFobSubleaseRedeliveryInfo spFobZzgcjr = null;
        if(serviceId!=null){
            spFobZzgcjr = mapperapper.selectById(serviceId);
        }else{
            spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryInfo>().eq(SpFobSubleaseRedeliveryInfo::getProceeInstanceId,id).ne(SpFobSubleaseRedeliveryInfo::getStatus,-2));
        }
        if(spFobZzgcjr==null){
            return null;
        }
        Map<String,Object> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(spFobZzgcjr), Object.class);
        if(spFobZzgcjr.getNoticeFile()!=null){
            String[] ids = spFobZzgcjr.getNoticeFile().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("noticeFile",spFobFileRecordList);
        }
        return map;
    }
    @Override
    public List<Long> findServieData(String processInstanceId){
        List<SpFobSubleaseRedeliveryInfo> spFobBaseBeans =  mapperapper.selectList(new LambdaQueryWrapper<SpFobSubleaseRedeliveryInfo>().eq(SpFobSubleaseRedeliveryInfo::getProceeInstanceId,processInstanceId).orderByDesc(SpFobSubleaseRedeliveryInfo::getCreateDate));
        List<Long> list = spFobBaseBeans.stream().map(SpFobBaseBean::getId).collect(Collectors.toList());
        return list;
    }
}
